/**
 * @file 播放器
 * @date 2017-07-27
 * @author sunshaocheng
 */

'use strict';

let $ = jQuery = window.jQuery = window.$ = require('jquery');

let Video = function(videoWrap) {
    this.videoWrap = videoWrap;
    this.videoCoverImg = this.videoWrap.find('[data-video-elem="coverImg"]');
    this.videoControl = this.videoWrap.find('[data-video-elem="control"]');
    this.videoCover = this.videoWrap.find('[data-video-elem="cover"]');
    this.player = this.videoWrap.find('[data-video-elem="player"]')[0];
}

Video.prototype = {
    constructor: Video,

    init() {
        this.bindEvent();
    },

    videoEleShow() {
        this.videoControl.show();
        this.videoCover.show();
        this.videoCoverImg.show();
    },

    videoEleHide() {
        this.videoControl.hide();
        this.videoCover.hide();
        this.videoCoverImg.hide();
    },

    bindEvent() {
        let _this = this;

        this.player.addEventListener('ended', function() { // 播放结束
            _this.videoEleShow();
        }, false);

        this.player.addEventListener('pause', function() { // 播放暂停
            _this.videoEleShow();
            _this.player.pause();
        }, false);

        this.player.addEventListener('play', function() { // 播放视频
            _this.videoEleHide();
        }, false);

        this.player.addEventListener('seeking', function() { // 拉动进度条
            _this.videoEleHide();
        }, false);

        this.videoControl.on('click', function() { // 点击播放按钮
            _this.videoEleHide();
            _this.player.play();
        });
    }
}

export default Video;
